// ============================== 导入依赖 ============================== //

import CORE from '@/CORE'
import { DemoRolePromiseClient as Client } from '@demo/proto/role/demo_role_grpc_web_pb'

import { ComQuery as ComClass } from '@demo/proto/common/ws_common_pb'
import {
  ListReq as ListClass,
  RoleDto as AddClass,
  RoleDto as EditClass,
  DeleteReq as DelClass,
} from '@demo/proto/role/demo_role_pb'

const CLIENT = CORE.grpcCLIENT(Client)
const COM = [ComClass, 'comQuery']

// ============================== 导出远程接口 ============================== //

export default {
  // —————————————————————————————— 列表 —————————————————————————————— //

  async getList(param, TIP = '查询列表') {
    const DTO = CORE.grpcDTO(ListClass, param, COM)
    const resp = await CORE.grpc(TIP, CLIENT, 'query', DTO)
    return [resp.comQuery.totalCount, resp.contentList || []]
  },

  // —————————————————————————————— 增 —————————————————————————————— //

  async add(one, TIP = '新增') {
    const DTO = CORE.grpcDTO(AddClass, one)
    await CORE.grpc(TIP, CLIENT, 'create', DTO)
    return CORE.grpcOK(TIP)
  },

  // —————————————————————————————— 改 —————————————————————————————— //

  async edit(one, TIP = '修改') {
    const DTO = CORE.grpcDTO(EditClass, one)
    await CORE.grpc(TIP, CLIENT, 'update', DTO)
    return CORE.grpcOK(TIP)
  },

  // —————————————————————————————— 删 —————————————————————————————— //

  async del({ id }, TIP = '删除') {
    const DTO = CORE.grpcDTO(DelClass, { id })
    await CORE.grpc(TIP, CLIENT, 'delete', DTO)
    return CORE.grpcOK(TIP)
  },
}
